package com.taotao.sso.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import com.taotao.sso.model.TbUser;
import com.taotao.sso.service.TbUserService;

@RestController
public class UserController {

	@Autowired
	private TbUserService tbUserService;

	/**
	 * 1.1.1. 检查数据是否可用
	 * 
	 * @param param
	 * @param type
	 * @return { status: 200 //200 成功 msg: "OK" // 返回信息消息 data: false //
	 *         返回数据，true：数据可用，false：数据不可用 }
	 * 
	 */
	@GetMapping("/user/check/{param}/{type}")
	public Map userCheck(@PathVariable String param, @PathVariable String type) {
		// type为类型，可选参数1、2、3分别代表username、phone、email
		Map result = new HashMap();
		result.put("status", 200);
		boolean isNull = tbUserService.userCheck(param, type);
		result.put("msg", "OK");
		result.put("data", isNull);
		return result;
	}

	/**
	 * 1.1.2. 用户注册
	 * 
	 * @param tbUser
	 * @return { status: 400 msg: "注册失败. 请校验数据后请再提交数据." data: null }
	 * 
	 */
	@PostMapping("/user/register")
	public Map userRegister(TbUser tbUser) {
		TbUser model = tbUserService.save(tbUser);
		Map result;
		if (model == null) {
			result = new HashMap();
			result.put("status", 400);
			result.put("msg", "注册失败. 请校验数据后请再提交数据.");
			result.put("data", null);
		} else {
			result = new HashMap();
			result.put("status", 200);
			result.put("msg", "注册成功.");
			result.put("data", tbUserService.save(tbUser));
		}
		return result;
	}

	/**
	 * 1.2. 用户登录
	 * 
	 * @param tbUser
	 * @return { status: 200 msg: "OK" data: "fe5cb546aeb3ce1bf37abcb08a40493e"
	 *         //登录成功，返回token }
	 * 
	 */
	@PostMapping("/user/login")
	public Map userLogin(TbUser tbUser) {
		String token = tbUserService.userLogin(tbUser);
		Map result = new HashMap();
		result.put("status", 200);
		result.put("msg", "OK");
		result.put("data", token);
		
		//TODO 登陆成功后 写入redis中 key是token  value是用户信息 设置过期时间 
		
		return result;
	}

	/**
	 * 1.3. 通过token查询用户信息
	 * 
	 * @param token
	 * @return { status: 200 msg: "OK" data:
	 *         "{"id":1,"username":"zhangzhijun","phone":"15800807944",
	 *         "email":"420840806@qq.com","created":1414119176000,"updated":1414119179000}"
	 *         }
	 * 
	 */
	@GetMapping("/user/token/{token}")
	public Map userToken(@PathVariable String token) {
		Map result = new HashMap();
		result.put("status", 200);
		result.put("msg", "OK");
		result.put("data", "fe5cb546aeb3ce1bf37abcb08a40493e");
		
		//TODO 在redis中查询数据 有返回 ， 重置过期时间， 无 则未登录
		return result;
	}

	/**
	 * 1.4. 安全退出
	 * 
	 * @param token
	 * @return { status: 200 msg: "OK" data: "" }
	 * 
	 */
	@GetMapping("/user/logout/{token}")
	public Map userLogout(@PathVariable String token) {
		Map result = new HashMap();
		result.put("status", 200);
		result.put("msg", "OK");
		result.put("data", "");
		//TODO 删除redis中的key
		return result;
	}

	@GetMapping("/users")
	public List<TbUser> findAll() {
		List<TbUser> result = tbUserService.findAll();
		return result;
	}

	@GetMapping("/users/{id}")
	public TbUser findByID(@PathVariable Long id) {
		TbUser result = tbUserService.findByID(id);
		return result;
	}

	@PostMapping("/users")
	public TbUser save(TbUser tbUser) {
		TbUser result = tbUserService.save(tbUser);
		return result;
	}

	@PutMapping("/user/{id}")
	public TbUser update(@PathVariable Long id, TbUser tbUser) {
		TbUser result = null;
		return result;
	}

	@DeleteMapping("/user/{id}")
	public String delete(@PathVariable Long id) {
		tbUserService.delete(id);
		String result = "";
		return result;
	}

}
